/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 用户模块api
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'configure', 'configureCom', 'jsDictFile', 'sgProjItemFile', 'laytpl', 'form', 'bluebird'], function (exports) {
    window.$ = window.jQuery = layui.$;
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var laytpl = layui.laytpl;
    var form = layui.form;
    var sgProjItemFile = layui.sgProjItemFile;
    var jsDictFile = layui.jsDictFile;

    //重组数据后渲染
    window.reArr = function (data) {
        var typeArr = [];
        var T;
        var newRes = {};
        $.each(data.result, function (i, v) {
            if (v.type !== T) {
                T = v.type;
                typeArr.push(T);
            }
        });
        $.each(typeArr, function (i, v) {
            newRes[v] = [];
            $.each(data.result, function (s, k) {
                if (k.type == v) {
                    newRes[v].push(k);
                }
            });
        });
        data.result = newRes;
        data.sgProjItemFile = sgProjItemFile;

        var getTpl = demo.innerHTML;
        var view = document.getElementById('view');
        laytpl(getTpl).render(data, function (html) {
            view.innerHTML = html;
            form.render();
        });
    };
    var specialTree = {
        getName: function (specialType) {
            // 获取专业名称
            if (specialType && specialType !== '') {
                $(jsDictFile.sgProjSpecType).each(function (index, item) {
                    $(item.childrens).each(function (index1, item1) {
                        if (item1.value === specialType) {
                            $('#professionalCategoryName').val(item1.name);
                            $('.chooseProfession .detail:eq(0)').text(item1.name);
                        }
                    });
                });
            }
        },
        getFormList: function (singleProjectId) {//获取要保存的表单数据列表
            var formList = [];
            $.each($('#view .formParam'), function (i, v) {
                var o = {
                    'singleProjId': singleProjectId,
                    'specialItemId': $(this).parent('.layui-input-inline').data('id'),
                    'specialItemValue': $(this).val(),
                    'specialTypeId': $(this).parent('.layui-input-inline').data('tid'),
                    'type': $(this).parent('.layui-input-inline').data('type')
                };
                formList.push(o);
            });
            return formList;
        },
        getTree: function (singleProjectId, specialType) {
            //获取表单列表数据
            configureCom.getAjax(configure.KCDJ_GET_ZYLIST_HTTP, {
                singleProjId: singleProjectId,
                specialTypeId: specialType
            }).then(function (data) {
                reArr(data);
            });
        }
    };
    exports('specialTree', specialTree);
});